package carTest.create;

import com.alibaba.fastjson.JSONObject;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.util.EntityUtils;

import java.io.IOException;

/**
 * @description:
 * @author: 张德长 dechang.zhang@ucarinc.com
 * @date: 16:37:24 2018/12/13
 * @version:2.0
 */
public class Operation {
    public static void add(Doc carTeestDoc, HttpClient client) {
        JSONObject carTeestDocJson = (JSONObject) JSONObject.toJSON(carTeestDoc);
        System.out.println(carTeestDocJson);
        String url = "http://cartest.10101111.com/interface/add";
        HttpPost httpPost = new HttpPost(url);
        httpPost.addHeader("Content-Type", "application/json;charset=utf-8");
        StringEntity entity = new StringEntity(carTeestDocJson.toString(), "utf-8");
        entity.setContentEncoding("UTF-8");
        entity.setContentType("application/json");
        httpPost.setEntity(entity);
        HttpResponse response = null;
        try {
            response = client.execute(httpPost);
        } catch (IOException e) {
            e.printStackTrace();
        }
        String json2 = null;
        try {
            json2 = EntityUtils.toString(response.getEntity(), "utf-8");
            System.out.println(json2);
        } catch (IOException e) {
            e.printStackTrace();
        }
        JSONObject jsonObject = JSONObject.parseObject(json2);
        if ("success".equals(jsonObject.getString("result"))) {
            //System.out.println(response);
        } else {
            System.out.println("新增 CARTEST 文档出错：" + JSONObject.toJSON(carTeestDoc));
        }
    }
}
